package com.zeking.dao;


import com.zeking.bean.User;
import com.zeking.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImp implements UserDao {
    QueryRunner qr;

    public UserDaoImp() {
        qr = new QueryRunner(DruidUtils.getDataSource());
    }

    @Override
    public User userLogin(String name, String password) {


        String sql = "select * from user where name = ? and password = ?";

        Object[] param = {name, password};

        User user = null;
        try {
            user = qr.query(sql, new BeanHandler<User>(User.class), param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println(user);

        return user;
    }

    @Override
    public List<User> getAllUser() {

        String sql = "select * from user";

        List<User> userList = new ArrayList<>();
        try {
            userList = qr.query(sql, new BeanListHandler<User>(User.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return userList;
    }

    @Override
    public BasePageBean<User> getUserByLimit(int currentPage, int pageSize) {

        int total;
        List<User> userList = new ArrayList<>();
        BasePageBean<User> userBasePageBean = new BasePageBean<>();

        String sqlCount = "select count(*) from user ";
        String sql = "select * from user limit ?,?";

        Object[] param = {(currentPage - 1) * pageSize, pageSize};

        try {
            total = ((Long) qr.query(sqlCount, new ScalarHandler<>())).intValue();
            userList = qr.query(sql, new BeanListHandler<User>(User.class), param);

            userBasePageBean.setTotal(total);
            userBasePageBean.setPageSize(pageSize);
            userBasePageBean.setCurrentPage(currentPage);

            int totalPage = 0;
            if (total % pageSize > 0) {
                totalPage = total / pageSize + 1;
            } else {
                totalPage = total / pageSize;
            }
            userBasePageBean.setTotalPage(totalPage);

            userBasePageBean.setData(userList);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return userBasePageBean;
    }

    @Override
    public void addUser(User user) {

        String sql = "insert into user(name,sex,birthday,email,note,isadmin,password) values(?,?,?,?,?,?,?)";
        Object[] param = {user.getName(),
                user.getSex(),
                user.getBirthday(),
                user.getEmail(),
                user.getNote(),
                user.isIsadmin(),
                user.getPassword()};

        try {
            qr.update(sql, param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    public void deleteUserByUid(int uid) {
        String sql = "delete from user where uid = ?";

        try {
            qr.update(sql, uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void updateUser(User user) {
        String sql = "update user set name=?,sex=?,birthday=?,email=?,note=?,isadmin=?,password=? where uid =?";

        Object[] param = {user.getName(),
                user.getSex(),
                user.getBirthday(),
                user.getEmail(),
                user.getNote(),
                user.isIsadmin(),
                user.getPassword(), user.getUid()};

        try {
            qr.update(sql, param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

//    @Override
//    public User userLogin(String name,String password) {
//        Connection connection = null;
//        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;
//        try {
//            // 1.获取数据库连接
//            connection = DbUtil.getConnection();
//            // 2.准备sql语句
//            String sql = "select * from user where name = ? and password = ?";
//            // 3.执行sql语句后获取结果并返回
//            preparedStatement = connection.prepareStatement(sql);
//            preparedStatement.setString(1, name);
//            preparedStatement.setString(2, password);
//            resultSet = preparedStatement.executeQuery();
//            if (resultSet.next()) {
//                User tu = new User(
//                        resultSet.getInt("uid"),
//                        resultSet.getString("name"),
//                        resultSet.getString("sex"),
//                        resultSet.getString("birthday"),
//                        resultSet.getString("email"),
//                        resultSet.getString("note"),
//                        resultSet.getBoolean("isadmin"),
//                        resultSet.getString("password"));
//                return tu; // 表示查找成功
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            // 4.释放相关的资源
//            try {
//                DbUtil.closeResource(connection, preparedStatement);
//                if (null != resultSet) {
//                    resultSet.close();
//                }
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//        return null; // 表示查找失败
//    }
}
